package com.medisign.sign.log.util;

import com.medisign.sign.log.config.SystemConfig;
import com.medisign.sign.log.pojo.TbSignLog;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @author ljt
 * @date 2025/4/1
 */
@Component
public class BaseDataSourceUtil {
    private static final Logger log = LoggerFactory.getLogger(BaseDataSourceUtil.class);

    private final SystemConfig systemConfig;

    public BaseDataSourceUtil(SystemConfig systemConfig) {
        this.systemConfig = systemConfig;
    }

    private Connection getBaseDataSourceConnection() {
        SystemConfig.DataSourceInfo dataSourceInfo = this.systemConfig.getDataSourceInfo();
        String driverName = dataSourceInfo.getDriverClassName();
        String dbURL = dataSourceInfo.getUrl();
        String userName = dataSourceInfo.getUsername();
        String userPwd = dataSourceInfo.getPassword();
        try {
            Class.forName(driverName);
            Connection con = DriverManager.getConnection(dbURL, userName, userPwd);
            return con;
        } catch (Exception e) {
            log.error("{}", e);
            throw new RuntimeException("");
        }
    }

    private void closeConnection(Connection con, Statement st, ResultSet rs) {
        if (null != rs)
            try {
                rs.close();
            } catch (SQLException e) {
                log.error("{}", e);
            }
        if (null != st)
            try {
                st.close();
            } catch (SQLException e) {
                log.error("{}", e);
            }
        if (null != con)
            try {
                con.close();
            } catch (SQLException e) {
                log.error("{}", e);
            }
    }

    public void save(TbSignLog signLog) {
        Connection connection = getBaseDataSourceConnection();
        PreparedStatement pstmt = null;
        try {
            pstmt = connection.prepareStatement("INSERT INTO tb_sign_log (file_code, start_time, end_time, unsign_data, signed_data, timestamp,sign_cert, log_path, strategy) VALUES (?,?,?,?,?,?,?,?,?)");
            pstmt.setString(1, signLog.getFileCode());
            pstmt.setTimestamp(2, new Timestamp(signLog.getStartTime().getTime()));
            pstmt.setTimestamp(3, new Timestamp(signLog.getEndTime().getTime()));
            pstmt.setString(4, signLog.getUnsignData());
            pstmt.setString(5, signLog.getSignedData());
            pstmt.setString(6, signLog.getTimestamp());
            pstmt.setString(7, signLog.getSignCert());
            pstmt.setString(8, signLog.getLogPath());
            pstmt.setString(9, signLog.getStrategy());
            pstmt.executeUpdate();
        } catch (SQLException e) {
            log.error("{}", e);
        } finally {
            closeConnection(connection, pstmt, null);
        }
    }
}